#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QRegExp>
#include <QRegExpValidator>

#include <fstream>
#include <QShortcut>

Widget::~Widget()
{
    delete ui;
}

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->label->setText("");
    ui->horizontalSlider->setMinimum(0);
    ui->horizontalSlider->setMaximum(100);
    ui->horizontalSlider->setSingleStep(10);
    ui->horizontalSlider->setValue(0);

    //设置快捷键
    QShortcut* shortcut1 = new QShortcut(this);
    shortcut1->setKey(QKeySequence("-"));
    connect(shortcut1,&QShortcut::activated,this,&Widget::subValue);

    QShortcut* shortcut2 = new QShortcut(this);
    shortcut2->setKey(QKeySequence("="));
    connect(shortcut2,&QShortcut::activated,this,&Widget::addValue);
}

void Widget::subValue()
{
    int value = ui->horizontalSlider->value();
    ui->horizontalSlider->setValue(value - 1);
}

void Widget::addValue()
{
    int value = ui->horizontalSlider->value();
    ui->horizontalSlider->setValue(value + 1);
}

void Widget::on_horizontalSlider_valueChanged(int value)
{
    ui->label->setText(QString::number(value));
}

//Widget::Widget(QWidget *parent)
//    : QWidget(parent)
//    , ui(new Ui::Widget)
//{
//    ui->setupUi(this);
//    ui->horizontalSlider->setMinimum(500);
//    ui->horizontalSlider->setMaximum(1500);
//    ui->horizontalSlider->setSingleStep(100);   //步长为100
//    ui->horizontalSlider->setValue(800);    //设置大小

//    ui->verticalSlider->setMinimum(500);
//    ui->verticalSlider->setMaximum(1500);
//    ui->verticalSlider->setSingleStep(100);
//    ui->verticalSlider->setInvertedAppearance(true);    //调整垂直滑动条方向
//    ui->verticalSlider->setValue(600);
//}

//void Widget::on_horizontalSlider_valueChanged(int width)
//{
//    QRect rect = this->geometry();
//    this->setGeometry(rect.x(),rect.y(),width,rect.height());
//}

//void Widget::on_verticalSlider_valueChanged(int height)
//{
//    QRect rect = this->geometry();
//    this->setGeometry(rect.x(),rect.y(),rect.width(),height);
//}

//Widget::Widget(QWidget *parent)
//    : QWidget(parent)
//    , ui(new Ui::Widget)
//{
//    ui->setupUi(this);
//    ui->dial->setRange(0,100);      //范围
//    ui->dial->setWrapping(true);    //"套圈"
//    ui->dial->setNotchesVisible(true);  //设置刻度可见
//    ui->dial->setValue(100);    //设置初始值
//}

//void Widget::on_dial_valueChanged(int value)
//{
//    ui->label->setText("当前的不透明度为：" + QString::number(value));
//    this->setWindowOpacity((double)value / 100);
//}

//Widget::Widget(QWidget *parent)
//    : QWidget(parent)
//    , ui(new Ui::Widget)
//{
//    ui->setupUi(this);
////    ui->spinBox->setMaximum(20);
////    ui->spinBox->setMinimum(0);
//    ui->spinBox->setRange(0,20);
//    ui->spinBox->setSuffix("瓶");
//}

//Widget::Widget(QWidget *parent)
//    : QWidget(parent)
//    , ui(new Ui::Widget)
//{
//    ui->setupUi(this);
//    std::fstream finput("D:\\A_Class_Code\\example.txt");
//    if(finput.is_open())
//    {
//        std::string input;
//        while(std::getline(finput,input))
//        {
//            ui->comboBox->addItem(QString::fromStdString(input));
//        }
//    }
//    ui->comboBox->setCurrentIndex(2); //默认选择第二个，超出范围(-1、100)则为空
//    ui->comboBox->setEditable(true);
//    finput.close();
//}

//Widget::Widget(QWidget *parent)
//    : QWidget(parent)
//    , ui(new Ui::Widget)
//{
//    ui->setupUi(this);
//    ui->comboBox->addItem("红烧肉");
//    ui->comboBox->addItem("糖醋排骨");
//    ui->comboBox->addItem("麻辣鱼");

//    ui->comboBox_2->addItem("素炒西兰花");
//    ui->comboBox_2->addItem("香辣土豆丝");
//    ui->comboBox_2->addItem("麻婆豆腐");

//    ui->comboBox_3->addItem("馒头");
//    ui->comboBox_3->addItem("米饭");
//}


//void Widget::on_pushButton_clicked()
//{
//    qDebug() << "荤菜:" << ui->comboBox->currentText()
//             << "素菜:" << ui->comboBox_2->currentText()
//             << "主食:" << ui->comboBox_3->currentText();
//}

//textEdit内容改变时，获取新内容
//void Widget::on_textEdit_textChanged()
//{
//    const QString content = ui->textEdit->toPlainText();
//    qDebug() << content;
//    ui->label->setText(content);
//}

////获取光标位置
//void Widget::on_textEdit_cursorPositionChanged()
//{
//    const QTextCursor cursor = ui->textEdit->textCursor();
//    qDebug() << "position:" <<cursor.position();
//}

////选中的内容
//void Widget::on_textEdit_selectionChanged()
//{
//    const QTextCursor cursor = ui->textEdit->textCursor();
//    qDebug() << "selectCursor:" << cursor.selectedText();
//}

//void Widget::on_checkBox_toggled(bool checked)
//{
//    if(checked)
//        ui->lineEdit->setEchoMode(QLineEdit::Normal);
//    else
//        ui->lineEdit->setEchoMode(QLineEdit::Password);
//}

//Widget::Widget(QWidget *parent)
//    : QWidget(parent)
//    , ui(new Ui::Widget)
//{
//    ui->setupUi(this);
//    ui->lineEdit->setAlignment(Qt::AlignLeft | Qt::AlignTop);//对齐方式
//    ui->lineEdit->setMaxLength(9);
//    ui->lineEdit->setDragEnabled(true);//可拖拽lineEdit中的内容
//}

//void Widget::on_lineEdit_cursorPositionChanged(int arg1, int arg2)
//{
//    qDebug() <<"光标位置变化：" << arg1 << " " << arg2;
//}

//void Widget::on_lineEdit_selectionChanged()
//{
//    qDebug() << "选中的文本发送变化";
//}

//Widget::Widget(QWidget *parent)
//    : QWidget(parent)
//    , ui(new Ui::Widget)
//{
//    ui->setupUi(this);
//    ui->lineEdit->setPlaceholderText("请输入格式正确的手机号");
//    ui->pushButton->setEnabled(false);  //初始按钮不可点击

//    //创建正则表达式对象
//    QRegExp express("^1\\d{10}$");
//    //创建一个验证器对象，并注册给lineEdit
//    ui->lineEdit->setValidator(new QRegExpValidator(express));
//}

//void Widget::on_lineEdit_textEdited(const QString &arg1)
//{
//    qDebug() << arg1;   //参数内容是输入框的值
//    QString tmp = arg1;
//    int pos = 0;
//    if(ui->lineEdit->validator()->validate(tmp,pos) == QValidator::Acceptable)
//    {
//        // 验证通过, 设置按钮的可⽤状态为启⽤
//        ui->pushButton->setEnabled(true);
//    }
//    else
//    {
//        ui->pushButton->setEnabled(false);
//    }
//}

//Widget::Widget(QWidget *parent)
//    : QWidget(parent)
//    , ui(new Ui::Widget)
//{
//    ui->setupUi(this);
//    //设置提示信息
//    ui->lineEdit_name->setPlaceholderText("请输入姓名");
//    ui->lineEdit_name->setClearButtonEnabled(false);//默认就没有清除，可不设置

//    ui->lineEdit_password->setPlaceholderText("请输入密码");
//    //设置清除按钮
//    ui->lineEdit_password->setClearButtonEnabled(true);
//    //密码隐藏输入
//    ui->lineEdit_password->setEchoMode(QLineEdit::Password);

//    ui->lineEdit_phone->setPlaceholderText("请输入手机号");
//    //手机号必须是11位的
//    ui->lineEdit_phone->setInputMask("000-0000-0000");
//    ui->lineEdit_password->setClearButtonEnabled(true);
//}

//void Widget::on_pushButton_clicked()
//{
//    QString gender = ui->radioButton_male->isChecked() ? "男" : "女";
//    qDebug() << "姓名：" << ui->lineEdit_name->text()
//             << "密码：" << ui->lineEdit_password->text()
//             << "性别：" << gender
//             << "电话：" << ui->lineEdit_phone->text();
//}


//void Widget::on_textEdit_redoAvailable(bool b)
//{
//    qDebug() << "redo:" << b;
//}

//void Widget::on_textEdit_undoAvailable(bool b)
//{
//    qDebug() << "undo:" << b;
//}

//void Widget::on_textEdit_copyAvailable(bool b)
//{
//    qDebug() << "copy:" << b;
//}


//void Widget::on_pushButton_clicked()
//{
//    ui->label_2->setText("祝你早日进入 " + ui->comboBox->currentText());
//}

//void Widget::on_comboBox_activated(const QString &arg1)
//{
//    qDebug() << arg1;
//}

//void Widget::on_comboBox_activated(int index)
//{
//    qDebug() << index;
//}

//void Widget::on_comboBox_editTextChanged(const QString &arg1)
//{
//    qDebug() << arg1;
//}

//void Widget::on_pushButton_clicked()
//{
//    QDateTime time1 = ui->dateTimeEdit->dateTime();
//    QDateTime time2 = ui->dateTimeEdit_2->dateTime();

////    int day = time1.daysTo(time2);  //注意：当时间跨天时，会算为1天
//    int day = (time2.secsTo(time1)) / 3600 / 24;
//    int hour = (time1.secsTo(time2)) / 3600 % 24;

//    ui->label->setText("相差" + QString::number(day)+ "天 " + QString::number(hour) + "小时");
//}


